/**
 * 
 */
package vue.desktop;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;

import model.cnf.Stat;

import vue.popup.JPopupCNFUse;
import vue.session.IGCNFUse;
import controller.ISessionManager;

/**
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGDeskCNFUse extends IGDeskItem implements MouseListener {

	private static final long		serialVersionUID	= 1L;

	private IGDeskCNFInformation	info;
	private IGDeskCNFStat			stat				= null;
	private IGCNFUse				cnfuse;

	protected JPopupCNFUse			rightMenu;

	public IGDeskCNFUse(IGCNFUse cnfuse, ISessionManager manager) {
		// TODO Auto-generated constructor stub
		this.cnfuse = cnfuse;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		// representation graphique de la cnf
		info = new IGDeskCNFInformation(cnfuse.getCnfuse().getCnf());
		add(info);

		// TODO GERER STAT

		// pop up menu
		rightMenu = new JPopupCNFUse(cnfuse, manager);
		addMouseListener(this);

		setBorder(BorderFactory.createLineBorder(Color.BLACK));

		setSize(getPreferredSize());
	}

	/**
	 * Return the {@link IGCNFUse} associate.
	 * 
	 * @return the {@link IGCNFUse} associate.
	 */
	public IGCNFUse getCnfuse() {
		return cnfuse;
	}

	public void refresh() {
		info.refresh(cnfuse.getCnfuse().getCnf());
		Stat s = cnfuse.getCnfuse().getCnf().getStat();
		if (s == null)
		{
			if (stat != null)
				remove(stat);
		}
		else
			if (stat != null)
				stat.refresh(s);
			else
			{
				stat = new IGDeskCNFStat(s);
				add(stat);
				setSize(getPreferredSize());
			}
		// TODO gerer stat

		rightMenu.refresh();
	}


	@Override
	public void mouseClicked(MouseEvent e) {
		cnfuse.notifySelected();
	}


	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}


}
